﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.WindowsMobile.PocketOutlook;
using System.Collections.ObjectModel;

namespace GSync
{
    public class OutlookMobileContactsProvider : OutlookMobileDataProvider<Outlook.Contact, Outlook.ContactCollection>
    {

        protected override InternalEntry ConvertToInternal(Outlook.Contact nativeContact)
        {
            InternalContact contact = new InternalContact();

            if( nativeContact.Properties.Contains("_ID") && !string.IsNullOrEmpty(nativeContact.Properties["_ID"] as string) )
                contact.ID = nativeContact.Properties["_ID"] as string;

            contact.Name = (nativeContact.LastName??"") + nativeContact.FirstName??nativeContact.AccountName;

            contact.Notes = nativeContact.Body;





    
            if(!string.IsNullOrEmpty( nativeContact.IM1Address ))
                contact.IMs.Add( nativeContact.IM1Address );

            if(!string.IsNullOrEmpty( nativeContact.IM2Address ))
                contact.IMs.Add( nativeContact.IM2Address );

            if(!string.IsNullOrEmpty( nativeContact.IM3Address ))
                contact.IMs.Add( nativeContact.IM3Address );


            if(!string.IsNullOrEmpty( nativeContact.MobileTelephoneNumber ))
                contact.PhoneNumbers.Add( nativeContact.MobileTelephoneNumber );

            if(!string.IsNullOrEmpty( nativeContact.HomeTelephoneNumber ))
                contact.PhoneNumbers.Add( nativeContact.HomeTelephoneNumber );

            if(!string.IsNullOrEmpty( nativeContact.CompanyTelephoneNumber ))
                contact.PhoneNumbers.Add( nativeContact.CompanyTelephoneNumber );




            if(!string.IsNullOrEmpty( nativeContact.Email1Address ))
                contact.EMails.Add( nativeContact.Email1Address );

            if(!string.IsNullOrEmpty( nativeContact.Email2Address ))
                contact.EMails.Add(nativeContact.Email2Address);

            if(!string.IsNullOrEmpty( nativeContact.Email3Address ))
                contact.EMails.Add(nativeContact.Email3Address);

            contact.InternalObject = nativeContact;


            return contact;
            
        }

        protected override void CopyToNative(InternalEntry entry, Outlook.Contact nativeContact)
        {
            InternalContact contact = entry as InternalContact;
            if (!nativeContact.Properties.Contains("_ID") || nativeContact.Properties["_ID"] == null)
                nativeContact.Properties.Add("_ID", typeof(string), Outlook.PimPropertyFlags.DoNotReplicate);
            nativeContact.Properties["_ID"] = contact.ID;

            nativeContact.FirstName = contact.Name;
            nativeContact.AccountName = contact.Name;
            //nativeEntry.Title = contact.Name;
            nativeContact.Body = contact.Notes;



            int i = 2;
            nativeContact.IM3Address = contact.IMs.Count > i ? contact.IMs[i] : string.Empty;
            nativeContact.Email3Address = contact.EMails.Count > i ? contact.EMails[i] : string.Empty;
            nativeContact.CompanyTelephoneNumber = contact.PhoneNumbers.Count > i ? contact.PhoneNumbers[i] : string.Empty;

            i = 1;
            nativeContact.IM2Address = contact.IMs.Count > i ? contact.IMs[i] : string.Empty;
            nativeContact.Email2Address = contact.EMails.Count > i ? contact.EMails[i] : string.Empty;
            nativeContact.HomeTelephoneNumber = contact.PhoneNumbers.Count > i ? contact.PhoneNumbers[i] : string.Empty;

            i = 0;
            nativeContact.Email1Address = contact.EMails.Count > i ? contact.EMails[i] : string.Empty;
            nativeContact.MobileTelephoneNumber = contact.PhoneNumbers.Count > i ? contact.PhoneNumbers[i] : string.Empty;
            nativeContact.IM1Address = contact.IMs.Count > i ? contact.IMs[i] : string.Empty;

        }

        protected override Microsoft.WindowsMobile.PocketOutlook.ContactCollection ItemCollection
        {
            get { return Session.Contacts.Items; }
        }


        public override void DeleteEntry(InternalEntry entry)
        {
            Outlook.Contact targerNativeEntry = FindNativeEntryByID(entry.ID);
            targerNativeEntry.Delete();
        }

        public override void AddEntry(InternalEntry entry)
        {
            Outlook.Contact newNativeContact = Session.Contacts.Items.AddNew();

            CopyToNative(entry, newNativeContact);

            newNativeContact.Update();
        }
    }
}
